package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-02-10
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {


    @Autowired
    private IApUserService apUserService;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if(dto.getStatus()!=null){
            queryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        IPage<ApUserRealname> page1 = this.page(page, queryWrapper);

        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }

    @Override
    @GlobalTransactional
    @Transactional
    public ResponseResult auth(AuthDto dto, int type) { // type：0 驳回  1通过
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname userRealname = this.getById(dto.getId());
        if(userRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(type==0){
//            只需要修改状态
            userRealname.setStatus(2); //"状态            0 创建中            1 待审核            2 审核失败            9 审核通过")
            this.updateById(userRealname);
        }else{
//            1、在自媒体数据库中的wm_user中生成数据
            ApUser apUser = apUserService.getById(userRealname.getUserId());
           WmUser wmUser = creatWmUser(userRealname,apUser);
//            2、在文章数据库中的 ap_author  中生成数据
          ApAuthor apAuthor =   createApAuthor(apUser,wmUser);

          wmUser.setApAuthorId(apAuthor.getId());
          mediaFeign.updateWmUser(wmUser);
//            3、修改用户状态
            userRealname.setStatus(9); //"状态            0 创建中            1 待审核            2 审核失败            9 审核通过")
            this.updateById(userRealname);
//            4、修改apUser的状态

            apUser.setFlag(1);   //"0 普通用户            1 自媒体人            2 大V"
            apUser.setIsIdentityAuthentication(true);// 已经实名认证
            apUserService.updateById(apUser);

        }
        return ResponseResult.okResult();
    }

    @Autowired
    private ArticleFeign articleFeign;

    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
//        apAuthor.setId(); //主键自增的 不用管
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());

        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if(responseResult.getCode()==0){
            return responseResult.getData();
        }else{
            throw new RuntimeException("远程调用失败");
        }

    }

    @Autowired
    private MediaFeign mediaFeign;

    private WmUser creatWmUser(ApUserRealname userRealname, ApUser apUser) {

        WmUser wmUser = new WmUser();
//        wmUser.setId();//主键自增的 不用管
        wmUser.setApUserId(apUser.getId());
//        wmUser.setApAuthorId(); //文章作者id ，一会儿创建完作者后再赋值
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
//        wmUser.setNickname(); //暂时空着
        wmUser.setImage(apUser.getImage());
//        wmUser.setLocation();//暂时空着
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(apUser.getStatus());
//        wmUser.setEmail();//暂时空着
        wmUser.setType(0);
        wmUser.setScore(0);
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if(responseResult.getCode()==0){
            return responseResult.getData();
        }else{
            throw new RuntimeException("远程调用失败");
        }
    }
}
